package com.sinba.itsm.web;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.accessibility.AccessibleExtendedComponent;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RestPasswordServlet
    extends HttpServlet
{
    public void doGet( HttpServletRequest request, HttpServletResponse response )
               throws ServletException, IOException
    {
        this.doPost( request, response );
    }

    public void doPost( HttpServletRequest request, HttpServletResponse response )
                throws ServletException, IOException
    {
        String userName = request.getParameter( "user" );
        String active = request.getParameter( "active" );
        AES aec = new AES(  );
        String[] infos = new String[2];

        try
        {
            String info = aec.decrypt( active );

            if ( info != null )
            {
                infos = info.split( "=" );
            }
        } catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace(  );
        }

        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );

        try
        {
            if ( ( infos[0] != null ) &&
                     ( infos[1] != null ) &&
                     sdf.parse( infos[0] ).before( new Date(  ) ) &&
                     userName.equals( infos[1] ) )
            {
                RequestDispatcher dispatcher =
                    request.getRequestDispatcher( "findPasswordUpdate.jsp?userName=" + userName );
                dispatcher.forward( request, response );
            } else
            {
                RequestDispatcher dispatcher = request.getRequestDispatcher( "findPasswordError.jsp" );
                dispatcher.forward( request, response );
            }
        } catch ( ParseException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace(  );
        }
    }
}
